package com.mp.approvalreq.entity;

import com.mp.approvalreq.entity.adjust.VersionInfo;
import com.mp.user.entity.User;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;
import java.util.Objects;

/**
 * @author xq
 */
public class Query {

    private Integer requestId;

    private Integer templateId;

    private Integer companyId;

    private Integer companyInfoId;

    private Integer contactId;

    private Integer applicant;

    private Integer applicantCiid;

    private String requestIds;

    private String templateIds;

    private String ancestorIds;

    private String companyInfoIds;

    private String contactIds;

    private String applicants;

    private Integer parentId;

    private String finishedSequence;

    private String financialStatusSequence;

    private Integer page;

    private Integer dataNum;

    private String orderBy;

    private String keywords;

    private Integer searchType;

    private Integer generation;

    private Date start;

    private Date end;

    private String startDate;

    private String endDate;

    private Integer type;

    private VersionInfo versionInfo;

    private String from;

    public Query() {
    }

    public Query(User user) {
        initUser(user);
    }

    public void initUser(User user) {
        this.companyId = user.getCompanyId();
        this.contactId = user.getContactId();
        this.companyInfoId = user.getCompanyInfoId();
    }

    public Integer getRequestId() {
        return requestId;
    }

    public void setRequestId(Integer requestId) {
        this.requestId = requestId;
    }

    public Integer getTemplateId() {
        return templateId;
    }

    public void setTemplateId(Integer templateId) {
        this.templateId = templateId;
    }

    public Integer getCompanyId() {
        return companyId;
    }

    public void setCompanyId(Integer companyId) {
        this.companyId = companyId;
    }

    public Integer getCompanyInfoId() {
        return companyInfoId;
    }

    public void setCompanyInfoId(Integer companyInfoId) {
        this.companyInfoId = companyInfoId;
    }

    public Integer getContactId() {
        return contactId;
    }

    public void setContactId(Integer contactId) {
        this.contactId = contactId;
    }

    public Integer getApplicant() {
        return applicant;
    }

    public void setApplicant(Integer applicant) {
        this.applicant = applicant;
    }

    public Integer getApplicantCiid() {
        return applicantCiid;
    }

    public void setApplicantCiid(Integer applicantCiid) {
        this.applicantCiid = applicantCiid;
    }

    public String getRequestIds() {
        return requestIds;
    }

    public void setRequestIds(String requestIds) {
        this.requestIds = requestIds;
    }

    public String getTemplateIds() {
        return templateIds;
    }

    public void setTemplateIds(String templateIds) {
        this.templateIds = templateIds;
    }

    public String getAncestorIds() {
        return ancestorIds;
    }

    public void setAncestorIds(String ancestorIds) {
        this.ancestorIds = ancestorIds;
    }

    public String getCompanyInfoIds() {
        return companyInfoIds;
    }

    public void setCompanyInfoIds(String companyInfoIds) {
        this.companyInfoIds = companyInfoIds;
    }

    public String getContactIds() {
        return contactIds;
    }

    public void setContactIds(String contactIds) {
        this.contactIds = contactIds;
    }

    public String getApplicants() {
        return applicants;
    }

    public void setApplicants(String applicants) {
        this.applicants = applicants;
    }

    public Integer getParentId() {
        return parentId;
    }

    public void setParentId(Integer parentId) {
        this.parentId = parentId;
    }

    public String getFinishedSequence() {
        return finishedSequence;
    }

    public void setFinishedSequence(String finishedSequence) {
        this.finishedSequence = finishedSequence;
    }

    public String getFinancialStatusSequence() {
        return financialStatusSequence;
    }

    public void setFinancialStatusSequence(String financialStatusSequence) {
        this.financialStatusSequence = financialStatusSequence;
    }

    public Integer getPage() {
        return page;
    }

    public void setPage(Integer page) {
        this.page = page;
    }

    public Integer getDataNum() {
        return dataNum;
    }

    public void setDataNum(Integer dataNum) {
        this.dataNum = dataNum;
    }

    public String getOrderBy() {
        return orderBy;
    }

    public void setOrderBy(String orderBy) {
        this.orderBy = orderBy;
    }

    public String getKeywords() {
        return keywords;
    }

    public void setKeywords(String keywords) {
        this.keywords = keywords;
    }

    public Integer getSearchType() {
        return searchType;
    }

    public void setSearchType(Integer searchType) {
        this.searchType = searchType;
    }

    public Integer getGeneration() {
        return generation;
    }

    public void setGeneration(Integer generation) {
        this.generation = generation;
    }

    public Date getStart() {
        return start;
    }

    public void setStart(Date start) {
        this.start = start;
    }

    public Date getEnd() {
        return end;
    }

    public void setEnd(Date end) {
        this.end = end;
    }

    public String getStartDate() {
        return startDate;
    }

    public void setStartDate(String startDate) {
        this.startDate = startDate;
    }

    public String getEndDate() {
        return endDate;
    }

    public void setEndDate(String endDate) {
        this.endDate = endDate;
    }

    public Integer getType() {
        return type;
    }

    public void setType(Integer type) {
        this.type = type;
    }

    public VersionInfo getVersionInfo() {
        return versionInfo;
    }

    public void setVersionInfo(VersionInfo versionInfo) {
        this.versionInfo = versionInfo;
    }

    public String getFrom() {
        return from;
    }

    public void setFrom(String from) {
        this.from = from;
    }

    public boolean querySelf() {
        return Objects.equals(contactId, applicant) || Objects.equals(companyInfoId, applicantCiid);
    }

    public boolean defaultOrderBy() {
        return StringUtils.isBlank(orderBy) || "0".equals(orderBy);
    }

    public boolean canHideListField() {
        try {
            if (versionInfo == null || StringUtils.isBlank(versionInfo.getPlatform())) {
                return false;
            }
            if (!"ios".equalsIgnoreCase(versionInfo.getPlatform()) && !"android".equalsIgnoreCase(versionInfo.getPlatform())) {
                return false;
            }
            return after(new int[]{4, 1, 3});
        } catch (Exception e) {
            return false;
        }
    }

    public boolean canHidePeopleList() {
        try {
            if (versionInfo == null || StringUtils.isBlank(versionInfo.getPlatform())) {
                return false;
            }
            if (!"ios".equalsIgnoreCase(versionInfo.getPlatform()) && !"android".equalsIgnoreCase(versionInfo.getPlatform())) {
                return false;
            }
            return after(new int[]{4, 3, 1});
        } catch (Exception e) {
            return false;
        }
    }

    public boolean canShowMultipleChosenAndRelatedComponent() {
        try {
            if (versionInfo == null || StringUtils.isBlank(versionInfo.getPlatform())) {
                return true;
            }
            if (!"prod".equalsIgnoreCase(versionInfo.getVersionType())) {
                return true;
            }
            if ("ios".equalsIgnoreCase(versionInfo.getPlatform())) {
                return after(new int[]{4, 7, 2});
            }
            if ("android".equalsIgnoreCase(versionInfo.getPlatform())) {
                return after(new int[]{4, 7, 3});
            }
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    private boolean after(int[] target) {
        String[] numbers = versionInfo.getVersionNum().split("\\.");
        int one = Integer.parseInt(numbers[0]);
        int two = Integer.parseInt(numbers[1]);
        int three = Integer.parseInt(numbers[2]);
        if (one != target[0]) {
            return one > target[0];
        }
        if (two != target[1]) {
            return two > target[1];
        }
        return three >= target[2];
    }

}
